package com.riche.codefactory.job.once;

import com.riche.codefactory.service.IQuestionSVC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;

import javax.annotation.Resource;

/**
 * 全量同步题目到 es
 *
 *
 */
// todo 取消注释开启任务
//@Component
@Slf4j
public class FullSyncQuestionToEs implements CommandLineRunner {

    @Resource
    private IQuestionSVC IQuestionSVC;

/*    @Resource
    private QuestionEsDao questionEsDao;*/

    @Override
    public void run(String... args) {
        /*// 全量获取题目（数据量不大的情况下使用）
        List<Question> questionList = IQuestionSVC.list();
        if (CollUtil.isEmpty(questionList)) {
            return;
        }
        // 转为 ES 实体类
        List<C2sQuestionEsDTO> c2sQuestionEsDTOList = questionList.stream()
                .map(C2sQuestionEsDTO::objToDto)
                .collect(Collectors.toList());
        // 分页批量插入到 ES
        final int pageSize = 500;
        int total = c2sQuestionEsDTOList.size();
        log.info("FullSyncQuestionToEs start, total ===================================> {}", total);
        for (int i = 0; i < total; i += pageSize) {
            // todo 注意同步的数据下标不能超过总数据量
            int end = Math.min(i + pageSize, total);
            log.info("sync from {} to {}", i, end);
            questionEsDao.saveAll(c2sQuestionEsDTOList.subList(i, end));
        }
        log.info("FullSyncQuestionToEs end, total ===================================> {}", total);*/
    }
}

